<?php

namespace App\Http\Controllers\Web;

use App\Caches\AliH5PayCreateCache;
use App\Caches\WechatH5PayCreateCache;
use App\Models\Games;
use App\Models\Orders;
use App\Services\GameChannelService;
use App\Services\PayService;
use App\Services\SwitchPay\SwitchPayBase;
use App\Services\SwitchPayService;
use Illuminate\Http\Request;

class SwitchPayController extends ControllerBase
{
    // 此接口核心用于安卓apk端
    public function index(Request $request, $trade_no)
    {
        $order = Orders::where('trade_no', $trade_no)->first();

        // 订单已完成支付流程
        if (!in_array($order->status, [Orders::PAY_STATUS_CREATE, Orders::PAY_STATUS_RUNING])) {
            return view('switch_pay.info', [
                'order' => $order,
            ]);
        }

        // 选择支付方式
        $open_pay_list = PayService::getInstance()->getOpenPayList($order->game_id, $order->game_channel_id);

        return view('switch_pay.index', [
            'order' => $order,
            'open_pay_list' => $open_pay_list,
        ]);
    }

    public function info(Request $request, $trade_no)
    {
        $order = Orders::where('trade_no', $trade_no)->first();

        return view('switch_pay.info', [
            'order' => $order,
        ]);

    }

    public function minigamePay(Request $request)
    {
        $trade_no = $request->input('trade_no');
        $order = Orders::where('trade_no', $trade_no)->first();

        // 订单已完成支付流程
        if (!in_array($order->status, [Orders::PAY_STATUS_CREATE, Orders::PAY_STATUS_RUNING])) {
            return view('switch_pay.info', [
                'order' => $order,
            ]);
        }

        // 选择支付方式
        return view('switch_pay.minigame', [
            'order' => $order,
        ]);

    }

    // 用户点击支付
    public function clickPay(Request $request)
    {
        $trade_no = $request->input('trade_no');
        $order    = Orders::where('trade_no', $trade_no)->first();
        if (!$order) {
            return yqerr(6020);
        }

        if (!in_array($order->status, [0,11])) {
            return yqerr(6041);
        }

        // 支付方式
        $pay_platform = $request->input('pay_platform');
        $payobj = SwitchPayBase::loadPay($pay_platform, $order->game_id, $order->game_channel_id);
        if (!$payobj) {
            return yqerr(6022);
        }

        // 保存支付方式
        $order->status       = Orders::PAY_STATUS_RUNING;
        $order->switch_pay   = 1;
        $order->pay_platform = $pay_platform;
        $order->save();

        return $payobj->payInit($order, $request);
    }

    // 基于微信公众号支付，调起授权成功返回获取openid
    public function wxpayJssdkAccessToken(Request $request)
    {
        $trade_no = $request->input('trade_no');
        $order    = Orders::where('trade_no', $trade_no)->first();
        if (!$order) {
            return yqerr(6020);
        }

        // 如果此订单已经被处理过，则跳过
        if ($order->status != Orders::PAY_STATUS_RUNING) {
            return yqerr(6021);
        }

        $pay_platform = Orders::PAY_PLATFORM_WECHAT_JSSDK;
        $payobj       = SwitchPayBase::loadPay($pay_platform, $order->game_id, $order->game_channel_id);
        if (!$payobj) {
            return yqerr(6022);
        }

        return $payobj->payInitHandel($order, $request);
    }

    // 基于微信H5支付跳转
    public function wxpayH5(Request $request)
    {
        $trade_no = $request->input('trade_no');
        $cache = WechatH5PayCreateCache::getInstance()->get($trade_no);
        if (empty($cache)) {
            return yqerr(6020);
        }

        return view('pay.wxh5', $cache);
    }

    // 基于支付宝H5支付跳转
    public function alipayH5(Request $request)
    {
        $trade_no = $request->input('trade_no');
        $cache = AliH5PayCreateCache::getInstance()->get($trade_no);
        if (empty($cache)) {
            return yqerr(6020);
        }

        return view('pay.alih5', $cache);
    }

    // 客户端支付回调
    public function callback(Request $request, $pay_platform, $game_channel_id)
    {
        return SwitchPayService::getInstance()->callback($request, $pay_platform, $game_channel_id, 'client');
    }
}
